from django.shortcuts import render
from django.template.context_processors import request
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Reader
from .Xlh import ReaderSerializer
import  jwt,datetime
from django.conf import settings
# Create your views here.

class ReaderLogin(APIView):
    def post(self, request):
        name=request.data.get('name')
        password=request.data.get('password')

        if not all([name,password]):
            return Response({
                'message':'不能为空'
            })


        token=jwt.encode({
            'name':name,
            'exp':datetime.datetime.utcnow() + datetime.timedelta(days=1)

        },settings.SECRET_KEY,algorithm='HS256')


        if Reader.objects.filter(name=name,password=password).exists():
            return Response({
                'message': '登录成功',
                'data':request.data,
                'token':token,
            })
